<?php

class Upload {

	static $errMsg = array(
		'上传成功',
		'超过了php.ini中设置的上传文件大小',
		'超过了form中设置的上传文件大小',
		'文件上传不完整',
		'没有选择文件',
		'临时目录不存在',
		'上传目录不可写',
		'UPLOAD STOPED BY EXTENSION'
	);

	public $error;
	public $errno;
	public $name;
	public $errors = array();
	public $errnos = array();
	public $names = array();
	private $sizeLimit;
	private $rename;
	private $path;

	private $allowExt = array('jpg', 'png', 'gif', 'jpeg', 'bmp');


	public function __construct($path, $sizeLimit = 2, $rename = false){
		if(!is_writeable($path)){
			$this->errno = 6;
			$this->error = self::$errMsg[$this->errno];
		}

		$this->path = $path;
		$this->sizeLimit = $sizeLimit * 1024 * 1024;
		$this->rename = $rename;
	}

	public function single($field){
		if($this->errno > 0){
			return false;
		}

		if(!isset($_FILES[$field])){
			return $result;
		}

		$file = $_FILES[$field];

		$this->errno = $file['error'];

		if ($file['error'] != 0) {
			$this->error = self::$errMsg[$file['error']];
			return false;
		}

		if ($file['size'] > $this->sizeLimit) {
			$this->errno = 9;
			$this->error = 'upload file is too big! ';
			return false;
		}

		$ext = array_pop(explode('.', $name));
		if(!in_array($ext, $this->allowExt)) {
			$this->errno = 9;
			$this->error = 'ext not allow';
			return false;
		}

		$this->name = $this->rename ? $this->genName($ext) : $file['name'];

		$path = $this->path . $this->name;

		if(file_exists($path)){
			$this->errno = 9;
			$this->error = '文件重名，重命名后再传';
			return false;
		}
		
		if (!move_uploaded_file($file['tmp_name'], $path)) {
			$this->errno = 9;
			$this->error = 'move file failed!';
			return false;
		}

		return true;
	}

	private function batch($field){
		if($this->errno > 0){
			return false;
		}

		if(!isset($_FILES[$field])){
			return $result;
		}

		$file = $_FILES[$field];
	
		$success = 0;		
		foreach($file['name'] as $index => $name){
			$this->errnos[$index] = $file['error'][$index];

			if ($file['error'][$index] != 0) {
				continue;
			}

			if ($file['size'][$index] > $this->sizeLimit) {
				$this->errnos[$index] = 9;
				$this->errors[$index] = 'upload file is too big!';
				continue;
			}

			$this->names[$index] = $this->rename ? $this->genName($file['name'][$index]) : $file['name'][$index];

			$path = $this->path . $this->names[$index];

			if(file_exists($path)){
				$this->errnos[$index] = 9;
				$this->errors[$index] = '文件重名，重命名后再传';
				continue;
			}
			
			if (!move_uploaded_file($file['tmp_name'][$index], $path)) {
				$this->errnos[$index] = 9;
				$this->errors[$index] = 'unknown error.';
				continue;	
			}

			$success += 1;
		}

		return $success == count($file['name']);
	}

	public function batchError(){
		$result = array();
		foreach($this->errnos as $i => $errno) {
			$result[] = array($this->names[$i], $errno, $this->errors[$i]);
		}
		return $result;
	}

	private function genName($ext){
		return date('YmdHis') . rand(1000, 9999) . '.' . $ext;
	}

	private function __destruct(){
		unset($this->errors, $this->errnos, $this->names);
	}
}

?>